﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Contacts.Services;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Domains;

namespace MvcMall.AdminWeb.Manage
{
    public partial class Index : ManageBasePage
    {
        private IIdentityService _identityService;
        private IAdminUserTask _adminUserTask;
        private List<AdminMenu> _menus;

        protected void Page_Load(object sender, EventArgs e)
        {
            _identityService = GetInstance<IIdentityService>();
            _adminUserTask = GetInstance<IAdminUserTask>();

            _menus = _adminUserTask.GetUserMenus(LogOnUser).ToList();
            if (!IsPostBack)
            {
                var muduls = _menus.Select(m => m.AdminModul).Distinct();
                rptModuls.DataSource = muduls;
                rptModuls.DataBind();
            }
        }

        protected void btnLogOff_Click(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
                _identityService.SignOut();
            Response.Redirect("~/Login.aspx");
        }

        protected void rptModuls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lblModulId = e.Item.FindControl("lblModulId") as Label;
                var modulId = Convert.ToInt32(lblModulId.Text);
                var rptMenus = e.Item.FindControl("rptMenus") as Repeater;
                var menus = _menus.Where(m => m.AdminModul.Id == modulId).ToList();
                rptMenus.DataSource = menus;
                rptMenus.DataBind();
            }
        }
    }
}